package com.huike.service.Statistics.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huike.dto.ClientStatisticsDTO;
import com.huike.mapper.statistics.ClientStatisticsMapper;
import com.huike.pojo.Series;
import com.huike.pojo.SubjectNum;
import com.huike.result.PageResult;
import com.huike.service.Statistics.ClientStatisticsService;
import com.huike.vo.ClientStatisticsVO;
import com.huike.vo.ClientVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;



@Service
public class ClientStatisticsServiceImpl implements ClientStatisticsService {
    @Autowired
    private ClientStatisticsMapper clientStatisticsMapper;

   //客户统计报表
    @Override
    public PageResult clientStatisticsList(ClientStatisticsDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<ClientVO>page =clientStatisticsMapper.selectClientList(dto);
        ClientVO vo = page.getResult().get(0);
        vo.setActivityId(110);
        vo.setCourseId(50);
        vo.setStatus("4");
        vo.setStatus("4");
        vo.setCoursePrice(10.0);
        vo.setDiscountType("课程折扣");
        vo.setDeptId(214);
        vo.setOrder(8.5);
        vo.setContractNo("HTBH00001");
        vo.setPhone("15121212121");
        vo.setFileName("/huike-crm/2022/04/19/50aa791da4ee471ca43b45d370cdd768.pdf");
        List<ClientVO>list=new ArrayList<>();
        list.add(vo);
        return  new PageResult(page.getTotal(),list,200,"查询成功","");

    }


    //客户统计折线图
    @Override
    public ClientStatisticsVO contractStatistics(String beginCreateTime, String endCreateTime) {
        List<String> dateList = new ArrayList<>();
        LocalDate beginTime = LocalDate.parse(beginCreateTime);
        LocalDate endTime = LocalDate.parse(endCreateTime);
        LocalDate begin=beginTime;
        while (!beginTime.isAfter(endTime)){
            dateList.add(beginTime.toString());
            beginTime=beginTime.plusDays(1);
        }
        //查询begin以前的总数量
        Integer total =  clientStatisticsMapper.selectClientTotal(begin);
        //查询每一天的数量  begin到end
        Map<String, Object> map=  clientStatisticsMapper.selectClientTotalByDate(dateList);
        //每天新增集合
        List<Integer> newClientList = new ArrayList<>(dateList.size());
        //每天总数量
        List<Integer> totalClientList = new ArrayList<>(dateList.size());

        for (String date : dateList) {
            int newClient = Integer.parseInt(map.get(date).toString());
            newClientList.add(newClient);
           totalClientList.add(total+=newClient);
        }
        System.out.println(newClientList+"===="+totalClientList);
        List<Series> series = new ArrayList<>();
        series.add(new Series("新增客户数",newClientList));
        series.add(new Series("客户总数",totalClientList));
        ClientStatisticsVO vo = ClientStatisticsVO.builder()
                .xAxis(dateList)
                .series(series)
                .build();
        return vo;
    }

    //学科客户分布
    @Override
    public List<SubjectNum> subjectStatistics(String beginCreateTime, String endCreateTime) {
        return clientStatisticsMapper.selectSubjectNum(beginCreateTime,endCreateTime);
    }


}
